package me.qixiaobo.controller;

import cn.hutool.core.util.ReflectUtil;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import me.qixiaobo.config.JsonViewConfig;
import me.qixiaobo.config.jackson.JSON;
import me.qixiaobo.config.jackson.JSONS;
import me.qixiaobo.dto.Book;
import me.qixiaobo.dto.User;
import me.qixiaobo.dto.UserVO1;
import me.qixiaobo.dto.UserVO;
import me.qixiaobo.enums.BaseEnum;
import me.qixiaobo.enums.BookFieldEnum;
import me.qixiaobo.enums.SystemEnvironmentEnum;
import me.qixiaobo.tran.annotation.Translation;
import me.qixiaobo.util.ResultGenerator;
import me.qixiaobo.util.ResultModel;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.UUID;

/**
 *
 */
@RestController
@RequestMapping("/user")
@Api(value = "用户信息操作 ", description = "用户基本信息操作API", tags = "user", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ApiResponses(value = {
        @ApiResponse(code = 200, message = "请求已完成"),
        @ApiResponse(code = 201, message = "资源成功被创建"),
        @ApiResponse(code = 400, message = "请求中有语法问题，或不能满足请求"),
        @ApiResponse(code = 401, message = "未授权客户机访问数据"),
        @ApiResponse(code = 403, message = "服务器接受请求，但是拒绝处理"),
        @ApiResponse(code = 404, message = "服务器找不到给定的资源；文档不存在"),
        @ApiResponse(code = 500, message = "服务器出现异常")}
)
//, 如果tags 中的值设置为中文, 那么 下面的方法名点击将不能被展
@Slf4j
public class UserController {

    static User user;

    static {
        user = new User();
        user.setId(Long.MAX_VALUE);
        user.setMail("78787@qq.com");
        user.setUserName("张三");
        user.setPhone("185********");
        user.setPassword("4asdas");
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
    }

    @GetMapping("/getDetailUser")
    @JsonView(JsonViewConfig.DetailView.class)//指明使用那个视图
    @ApiOperation(value = "获取用户多个字段的信息", notes = "")
    public ResultModel<User> getDetailUser(@ApiParam(name = "userId", value = "用户id", required = true) @RequestParam String userId) {

        return ResultGenerator.genSuccessResult(user);
    }

    // @ApiImplicitParam：用在@ApiImplicitParams注解中
    // paramType：参数放在哪个地方
    // name：参数代表的含义
    // value：参数名称
    // dataType： 参数类型，有String/int，无用
    // required ： 是否必要
    // defaultValue：参数的默认值


    @GetMapping("/getSimpleUser")
    @JsonView(JsonViewConfig.SimpleView.class)//指明使用那个视图
    @ApiOperation(value = "获取用户少量字段信息", notes = "")
    @ApiImplicitParam(name = "name", value = "用户名", dataType = "string", paramType = "query", example = "xingguo")
    public ResultModel<User> getSimpleUser(@RequestParam String name) {
        user.setBook(getBook());
        return ResultGenerator.genSuccessResult(user);
    }

    @PostMapping("/saveUser")
    @ApiOperation(value = "新增用户信息", notes = "")
    public ResultModel<User> saveUser(@RequestBody User user) {
        user.setId(1L);
        user.setMail("78787@qq.com");
        user.setUserName("张三");
        user.setPhone("185********");
        user.setPassword(UUID.randomUUID().toString());
        user.setCreateTime(new Date());
        return ResultGenerator.genSuccessResult(user);
    }


    /**
     * book
     */
    private Book getBook() {
        Book book = new Book();
        book.setBookId(RandomUtils.nextLong(1, 100) + "");
        book.setBookCount(10);
        book.setBookName("bookNM");
        return book;
    }

    /**
     * 使用自定义注解实现响应字段的过滤
     *
     * @return
     */
    @GetMapping("/getUser")
    @ApiOperation(value = "JSON:获取用户的信息,不包括: userName,createTime", notes = "")
    @JSON(type = User.class, filter = "userName,createTime")
    public ResultModel<User> getUser() {
        user.setId(RandomUtils.nextLong(1, 100));
        user.setBook(getBook());
        return ResultGenerator.genSuccessResult(user);
    }


    /**
     * 使用自定义注解实现响应字段的过滤
     *
     * @return
     */
    @GetMapping("/getUserByFilter")
    @ApiOperation(value = "JSON:获取用户的信息,不包括:createTime,userName, Book里不包括:bookId", notes = "")
    @JSONS({@JSON(type = User.class, filter = "createTime,userName"), @JSON(type = Book.class, filter = "bookId")})
    public ResultModel<User> getUserByFilter() {
        user.setId(RandomUtils.nextLong(10, 200));
        user.setBook(getBook());
        return ResultGenerator.genSuccessResult(user);
    }


    @GetMapping("/getUserContainField")
    @ApiOperation(value = "JSON:获取用户的信息,只有:userName,Book所有字段", notes = "")
    @JSON(type = User.class, include = "userName,book")
    public ResultModel<User> getUserContainField() {
        user.setId(RandomUtils.nextLong(1, 100));
        user.setBook(getBook());
        return ResultGenerator.genSuccessResult(user);
    }

    @GetMapping("/getUserBookContainField")
    @ApiOperation(value = "JSON:获取用户的信息,只有:userName,id,phone ,Book里只有:bookId", notes = "")
    @JSONS({@JSON(type = User.class, include = "id,userName,phone,book"), @JSON(type = Book.class, include = "bookId")})
    public ResultModel<User> getUserBookContainField() {
        user.setBook(getBook());
        return ResultGenerator.genSuccessResult(user);
    }

    @GetMapping("/getAllParams")
    @ApiOperation(value = "JSON:获取用户的信息,所有字段", notes = "")
    public ResultModel<User> getAllParams() {
        return ResultGenerator.genSuccessResult(user);
    }

    @GetMapping("/getUserInfo")
    @ApiOperation(value = "JSON:获取用户的信息,忽略指定的字段", notes = "")
    public ResultModel<UserVO1> getUserInfo() {
        UserVO1 userVO1 =new UserVO1();
        BeanUtils.copyProperties(user, userVO1);
        return ResultGenerator.genSuccessResult(userVO1);
    }

    @GetMapping("/getEnums")
    @ApiOperation(value = "JSON:枚举类", notes = "")
    public ResultModel<UserVO> getEnums() {
        UserVO userVO=new UserVO();
        SystemEnvironmentEnum dev = SystemEnvironmentEnum.DEV;
        userVO.setUserName("测试");
        userVO.setEnvironmentEnum(dev);
        return ResultGenerator.genSuccessResult(userVO);
    }


    @GetMapping("/getData")
    @ApiOperation(value = "通过字段,翻译", notes = "")
    public ResultModel<Book> getData() {
        Book book = getBook();

        String fieldName = "bookName";
        Object fieldValue = ReflectUtil.getFieldValue(book, fieldName);
        String descByCode = BaseEnum.getDescByCode(String.valueOf(fieldValue),BookFieldEnum.class);
        ReflectUtil.setFieldValue(book,fieldName,descByCode);

        return ResultGenerator.genSuccessResult(book);
    }

}
